﻿using System;
using System.Collections.Generic;

namespace AntBot
{
	public class Bot
	{
		public State State;

		public void PlayGame()
		{
			IOManager ioManager = new IOManager();

			State = new State(ioManager.ReadSettings());
			ioManager.SendReady();

			do
			{
				DataSet dataSet = ioManager.ReadTurn();
				if (ioManager.EndOfGame) break;

				State.Update(dataSet);
				Logger.Log(State.ToString());

				DoTurn(ioManager);

				ioManager.SendReady();
			} while (true);
		}

		public void DoTurn(IOManager ioManager)
		{
			Logger.Log("ход " + State.TurnNumber);
			try
			{
				if (State.MyAnts == null || State.MyAnts.Count == 0) return;

				FoodCollector foodCollector = new FoodCollector(State);
				List<Move> moves = foodCollector.Search();
				foreach (Move move in moves)
				{
					if (move.TurnsBefore != 0) continue;
					ioManager.SendStep(new Step(move.Route.Path[0], move.GetFirstStep()));
				}
				/*foreach (Location location in State.MyAnts)
				{
					for (int d = 0; d < State.NDIRECTIONS; d++)
					{
						Location newLocation = State.GetNewLocation(location, d);

						if (State.Map.Get(newLocation.Col, newLocation.Row).IsWater) continue;
						if (State.Map.Get(newLocation.Col, newLocation.Row).Ant > State.NO_ANT) continue;
						ioManager.SendStep(new Step(location, d));
						break;
					}
				}*/
			}
			catch (Exception e)
			{
				Logger.Log(e.ToString());
			}
			
		}
	}
}
